package com.lanlian.cayman.widget;

import android.app.Activity;
import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lanlian.cayman.R;
import com.lanlian.cayman.bean.GearBean;
import com.lanlian.cayman.utils.MResUtil;
import com.lanlian.cayman.utils.NumberManager;

import java.util.ArrayList;

/**
 * 盘口控件
 */
public class GearView extends LinearLayout{
    ArrayList<GearBean> buy=new ArrayList<>();
    ArrayList<GearBean> sell=new ArrayList<>();


    ArrayList<TextView> buy_id=new ArrayList<>();
    ArrayList<TextView> buy_price=new ArrayList<>();
    ArrayList<TextView> buy_num=new ArrayList<>();
    ArrayList<View> buy_view=new ArrayList<>();

    ArrayList<TextView> sell_id=new ArrayList<>();
    ArrayList<TextView> sell_price=new ArrayList<>();
    ArrayList<TextView> sell_num=new ArrayList<>();
    ArrayList<View> sell_view=new ArrayList<>();

    LinearLayout ll_sell;
    LinearLayout ll_buy;
    Activity context;
    /**
     * 上涨的颜色
     */
    int color_up=0xffEB5365;
    /**
     * 下跌的颜色
     */
    int color_down=0xff50B861;
    /**
     * 平的颜色
     */
    int color_ping=0xffffffff;
    String last_price="1";

    int gearCount=5;
    public GearView(Context context) {
        super(context);
        initView();

    }
    public GearView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context=(Activity) context;
        initView();
    }
    private  void initView()
    {
        LinearLayout ll=new LinearLayout(context);
        LayoutInflater inflater=context.getLayoutInflater();
        View view = inflater.inflate(R.layout.view_gear, ll,false);
        addView(view);
        ll_buy=findViewById(R.id.ll_buy);
        ll_sell=findViewById(R.id.ll_sell);

        initGear();
    }
    private void initGear()
    {
        View view=null;
        LayoutInflater inflater=context.getLayoutInflater();
        LinearLayout ll=new LinearLayout(context);
        TextView tv_id=null;
        TextView tv_price=null;
        TextView tv_num=null;
        for (int i=0;i<gearCount;i++)
        {
            view = inflater.inflate(R.layout.view_gear_item, ll,false);
            tv_id=view.findViewById(R.id.tv_id);
            tv_price=view.findViewById(R.id.tv_price);
            tv_num=view.findViewById(R.id.tv_num);

            tv_id.setText("卖"+(5-i));
            tv_price.setText("--");
            tv_num.setText("--");
            tv_price.setTextColor(MResUtil.getPingColor());

            sell_id.add(tv_id);
            sell_price.add(tv_price);
            sell_num.add(tv_num);

            sell_view.add(view);
            ll_sell.addView(view);
        }
        for (int i=0;i<gearCount;i++)
        {
            view = inflater.inflate(R.layout.view_gear_item, ll,false);
            tv_id=view.findViewById(R.id.tv_id);
            tv_price=view.findViewById(R.id.tv_price);
            tv_num=view.findViewById(R.id.tv_num);

            tv_id.setText("买"+(i+1));
            tv_price.setText("--");
            tv_num.setText("--");
            tv_price.setTextColor(MResUtil.getPingColor());

            buy_id.add(tv_id);
            buy_price.add(tv_price);
            buy_num.add(tv_num);
            ll_buy.addView(view);
            buy_view.add(view);
        }
    }

    /**
     * 设置上一个交易日收盘价格
     */
    public void setLastDayPrice(String price)
    {
        this.last_price=price;
    }
    public void refresh()
    {
        setData(buy,sell);
    }
    public void setData(ArrayList<GearBean> buy, ArrayList<GearBean> sell)
    {

        this.buy=buy;
        this.sell=buy;
        if(getVisibility()==GONE)
        {
            return;
        }

        View view=null;
        TextView tv_id=null;
        TextView tv_price=null;
        TextView tv_num=null;

         for(int i=0;i<buy_view.size();i++)
         {
 //            view=buy_view.get(i);
//             tv_id=view.findViewById(R.id.tv_id);
//             tv_price=view.findViewById(R.id.tv_price);
//             tv_num=view.findViewById(R.id.tv_num);
//             countFontSize(tv_id,sell.get(i).price.length());
//             countFontSize(tv_price,sell.get(i).price.length());
//             countFontSize(tv_num,sell.get(i).price.length());
             tv_id=buy_id.get(i);
             tv_price=buy_price.get(i);
             tv_num=buy_num.get(i);

             tv_id.setText("买"+(i+1));
             tv_price.setText(buy.get(i).price);
             tv_num.setText(buy.get(i).num);
             if(NumberManager.isDaYu(buy.get(i).price,"0")==0)
             {
                 if(NumberManager.isDaYu(buy.get(i).num,"0")==0)
                 {
                     tv_price.setText("--");
                     tv_num.setText("--");
                 }
                 tv_price.setTextColor(MResUtil.getPingColor());
             }
             else {

                 if (NumberManager.isDaYu(buy.get(i).price, last_price) > 0) {
                     tv_price.setTextColor(MResUtil.getUpColor());
                 } else if (NumberManager.isDaYu(buy.get(i).price, last_price) < 0) {
                     tv_price.setTextColor(MResUtil.getDownColor());
                 } else {
                     tv_price.setTextColor(MResUtil.getPingColor());
                 }
             }

         }

        for(int i=0;i<sell_view.size();i++)
        {
//            view=sell_view.get(sell_view.size()-i-1);
//            tv_id=view.findViewById(R.id.tv_id);
//            tv_price=view.findViewById(R.id.tv_price);
//            tv_num=view.findViewById(R.id.tv_num);
//            countFontSize(tv_id,sell.get(i).price.length());
//            countFontSize(tv_price,sell.get(i).price.length());
//            countFontSize(tv_num,sell.get(i).price.length());

            tv_id=sell_id.get(sell_view.size()-i-1);
            tv_price=sell_price.get(sell_view.size()-i-1);
            tv_num=sell_num.get(sell_view.size()-i-1);

            tv_id.setText("卖"+(i+1));
            tv_price.setText(sell.get(i).price);
            tv_num.setText(sell.get(i).num);



            if(NumberManager.isDaYu(sell.get(i).price,"0")==0)
            {
                if(NumberManager.isDaYu(sell.get(i).num,"0")==0)
                {
                    tv_price.setText("--");
                    tv_num.setText("--");
                }
                tv_price.setTextColor(MResUtil.getPingColor());
            }
            else {


                if (NumberManager.isDaYu(sell.get(i).price, last_price) > 0) {
                    tv_price.setTextColor(MResUtil.getUpColor());
                } else if (NumberManager.isDaYu(sell.get(i).price, last_price) < 0) {
                    tv_price.setTextColor(MResUtil.getDownColor());
                } else {
                    tv_price.setTextColor(MResUtil.getPingColor());
                }
            }
        }
    }
    private  void countFontSize(TextView tv,int lenght)
    {

          if(lenght<=4)
          {
              tv.setTextSize(15);
          }
          else if(lenght>4&&lenght<=7)
          {
              tv.setTextSize(13);
          }
          else if(lenght>7)
          {
              tv.setTextSize(11);
          }

    }
}
